package cn.virens.support;

import cn.virens.TioUtil;
import cn.virens.app.main.ComponentTimer;

public class ExecutorRunnableHeat extends ComponentTimer<TCPServerImplHeartbeat> {

	public ExecutorRunnableHeat(TCPServerImplHeartbeat cmpt, String name) {
		super(cmpt, name);
	}

	@Override
	protected void doRun() throws Exception {
		cmpt.channelForEach((channelContext) -> {
			Integer heartTimesVal = TioUtil.heartTimes(channelContext, (h) -> h.get());
			Integer heartIdleVal = TioUtil.heartIdle(channelContext, (h) -> h.incrementAndGet());

			// 日志打印心跳
			cmpt.logger.debug("心跳检查({}):空闲->{}/心跳->{}", TioUtil.name(channelContext), heartIdleVal, heartTimesVal);

			// 检查心跳值
			if (heartIdleVal > cmpt.heartidle) {
				cmpt.channelTimeout(channelContext, heartIdleVal, heartTimesVal);
			}
		});
	}
}
